package com.cskaoyan.shopping.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.impl.cache.CacheManager;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author fan
 * @Description:
 * @since 2023/01/10 10:43
 */
@Service
@Transactional
public class ICartServiceImpl implements ICartService {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    ItemMapper itemMapper;

    @Override
    public List<CartProductDto> getCartListById(Long userId) {
        String id = String.valueOf(userId);
        RMap<String, CartProductDto> map = redissonClient.getMap(id);
        Set<String> set = map.keySet();
        ArrayList<CartProductDto> list = new ArrayList<>();
        for (String productId : set) {
            CartProductDto product = map.get(productId);
            list.add(product);
        }
        return list;
    }

    @Override
    public void addToCart(CartRequest request) {
        String userId = String.valueOf(request.getUserId());
        RMap<String, CartProductDto> map = redissonClient.getMap(userId);

        String productId = String.valueOf(request.getProductId());
        // 查找商品表中商品
        Item item = itemMapper.selectByPrimaryKey(productId);

        CartProductDto product = null;
        // 如果购物车不存在该商品，则直接添加
        if (!map.containsKey(productId)) {
            product = new CartProductDto();
            product.setProductId(Long.valueOf(productId));
            product.setSalePrice(item.getPrice());
            product.setProductNum(Long.valueOf(request.getProductNum()));
            product.setLimitNum(Long.valueOf(item.getLimit_num()));
            product.setChecked("true");
            product.setProductName(item.getTitle());
            product.setProductImg(item.getImageBig());
        } else {
            // 如果购物车存在该商品，则添加数量
            product = map.get(productId);
            product.setProductNum(product.getProductNum() + request.getProductNum());
        }
        map.put(productId, product);
    }

    @Override
    public void updateCartNum(CartRequest request) {
        @NotNull Long userId = request.getUserId();
        String id = String.valueOf(userId);
        RMap<String, CartProductDto> map = redissonClient.getMap(id);
        CartProductDto cartProductDto = map.get(String.valueOf(request.getProductId()));
        cartProductDto.setProductNum(request.getProductNum().longValue());
        cartProductDto.setChecked(request.getChecked());
        map.put(String.valueOf(request.getProductId()),cartProductDto);
    }

    @Override
    public void checkAllCartItem(CheckAllItemRequest request) {
        String userId = String.valueOf(request.getUserId());
        RMap<String, CartProductDto> map = redissonClient.getMap(userId);
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            CartProductDto product = map.get(key);
            product.setChecked(request.getChecked());
            map.put(key,product);
        }
    }

    @Override
    public void deleteCartItem(Long uid, Long itemId) {
        String id = String.valueOf(uid);
        RMap<String, CartProductDto> map = redissonClient.getMap(id);
        map.remove(String.valueOf(itemId));
    }

    @Override
    public void deleteCheckedItem(Long uId) {
        /*String userId = String.valueOf(uId);
        RMap<String, CartProductDto> map = redissonClient.getMap(userId);
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            CartProductDto productDto = map.get(key);
            // 若被选中，则删除
            // 出现bug原因: CHECKED应该为"true"
            if (CHECKED.equals(productDto.getChecked())){
                map.remove(String.valueOf(productDto.getProductId()));
            }
        }*/

        String userId = String.valueOf(uId);
        RMap<String, CartProductDto> map = redissonClient.getMap(userId);
//        判定购物车是否为空,为空就返回
        if (map.isEmpty()){
            return;
        }
//        把被选中的货物id就塞到list中
        ArrayList<String> list = new ArrayList<>();
        for (CartProductDto value : map.values()) {
            if (Objects.equals(value.getChecked(),"true")){
                list.add(value.getProductId().toString());
            }
        }
//        将map中被选中的商品删除
        for (String key : list) {
            map.remove(key);
        }


    }
}
